package com.as.measurement.server.core;

import java.io.IOException;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext;

import com.as.measurement.model.ClientInterface;
import com.as.measurement.model.UserInterfaces;
import com.as.measurement.server.ServerLauncher;
import com.as.measurement.server.service.ServerService;








public class ServerCore implements ServerService {
	
	

	/**
	 * Logger
	 */
	private Log _logger = LogFactory.getLog(getClass().getName());
	
	ServerLauncher sl ;
	
	
	
	
	private void init (){
		
		
		// init iperf server TODO
		
		// create results object to return to the gwt server side
		
		// get the measure and update the measurement object...
		
	 sl = new ServerLauncher();
	 sl.start();

		
	}

	
	
	protected void activate(ComponentContext context) {
		_logger.info("Activated component:" + getClass().getName());
		
		init();
		_logger.info(" Server initialized !" );
	}

	protected void deactivate(ComponentContext context) {
		_logger.info("Desactivate component:" + getClass().getName());
		
		try {
			sl.stopAllThreads();
		} catch (IOException e) {
	
			_logger.error("Error trying to close sockets and destroying threads ", e);
		}
		sl.stop();
		_logger.info("Desactivate component correctly:" + getClass().getName());
	}



	@Override
	public HashMap<String,ClientInterface> getQoSParams () {
		// TODO Auto-generated method stub
		HashMap<String, ClientInterface> ht = UserInterfaces.getInstance().userInterfaces;
		return ht;
	}
	

}
